// 创建整个应用的路由器
// 引入vue-router
import VueRouter from 'vue-router'

import Register from '../pages/Register.vue'
import Login from '../pages/Login.vue'
import Time from '../pages/Time.vue'
import Home from '../pages/Home.vue'
// 引入需要使用路由的组件
// 创建一个路由器
const router = new VueRouter({
    // 配置需要使用路由的组件,以对象的形式
    routes: [
        {
            path: '*',
            redirect: '/'
        },
        {
            name: 'login',
            path: '/',
            component: Login,
            meta: {
                title: '登录'
            }

        },
        {
            path: '/register',
            component: Register,
            mate: {
                title: '注册'
            }
        },
        {
            path: '/home',
            component: Home,
            meta: {
                title: '主页',
                isLogin: true
            },
        }
    ]
})
// 路由前置守卫
router.beforeEach((to, from, next) => {
    if (to.meta.isLogin) {
        let username = to.query.username
        let password = to.query.password
        let users = JSON.parse(localStorage.getItem('users'))
        
        if (user.name === inputName && user.password === inputPsd || from.path === '/home' || from.path === '/home/todos' || from.path === '/home/countdown') {
            next();
        } else {
            next('/');
        }
    } else {
        next();
    }
    }
})

// 将router暴露出去
export default router;
